package com.ruoyi.web.controller.postComplain;

import java.util.List;

import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.post.domain.Post;
import com.ruoyi.post.service.IPostService;
import com.ruoyi.topic.domain.Topic;
import com.ruoyi.topic.service.ITopicService;
import com.ruoyi.topicComplain.domain.TopicComplain;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postComplain.domain.PostComplain;
import com.ruoyi.postComplain.service.IPostComplainService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 帖子举报 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-10-14
 */
@Controller
@RequestMapping("/postComplain/postComplain")
public class PostComplainController extends BaseController
{
    private String prefix = "postComplain/postComplain";

	private String prefix2 = "postComplain/postComplainRecord";
	
	@Autowired
	private IPostComplainService postComplainService;

	@Autowired
	private ITopicService topicService;

	@Autowired
	private IPostService postService;

	@Autowired
	private IUserService userService;
	
	@RequiresPermissions("postComplain:postComplain:view")
	@GetMapping()
	public String postComplain(ModelMap mmap)
	{
		Topic topic = new Topic();
		List<Topic> topicList = topicService.selectTopicList(topic);
		mmap.put("topicList", topicList);
		return prefix + "/post";
	}

	@RequiresPermissions("topicComplain:topicComplain:view")
	@GetMapping("/record")
	public String postComplainRecord(ModelMap mmap)
	{
		Topic topic = new Topic();
		List<Topic> topicList = topicService.selectTopicList(topic);
		mmap.put("topicList", topicList);
		return prefix2 + "/postComplain";
	}
	
	/**
	 * 查询帖子举报列表
	 */
	@RequiresPermissions("postComplain:postComplain:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PostComplain postComplain)
	{
		startPage();
		postComplain.setComplainVerify("0");
        List<PostComplain> list = postComplainService.selectPostComplainList(postComplain);
		return getDataTable(list);
	}

	/**
	 * 查询话题举报列表
	 */
	@RequiresPermissions("topicComplain:topicComplain:list")
	@PostMapping("/recordList")
	@ResponseBody
	public TableDataInfo recordList(PostComplain postComplain)
	{
		startPage();
		postComplain.setValid(2);
		List<PostComplain> list = postComplainService.selectPostComplainList(postComplain);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出帖子举报列表
	 */
	@RequiresPermissions("postComplain:postComplain:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PostComplain postComplain)
    {
    	List<PostComplain> list = postComplainService.selectPostComplainList(postComplain);
        ExcelUtil<PostComplain> util = new ExcelUtil<PostComplain>(PostComplain.class);
        return util.exportExcel(list, "postComplain");
    }
	
	/**
	 * 新增帖子举报
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存帖子举报
	 */
	@RequiresPermissions("postComplain:postComplain:add")
	@Log(title = "帖子举报", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PostComplain postComplain)
	{		
		return toAjax(postComplainService.insertPostComplain(postComplain));
	}

	/**
	 * 修改帖子举报
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		String postContent = "无";
		String userName = "";
		PostComplain postComplain = postComplainService.selectPostComplainById(id);
		Integer userId = postComplain.getComplainUserId();
		Integer postId = postComplain.getPostId();
		Post post = postService.selectPostById(postId);
		if (post != null) {
			postContent = post.getPostContent();
		}
		User user = userService.selectUserById(userId);
		if (user != null) {
			userName = user.getNickname();
		}
		mmap.put("postContent", postContent);
		mmap.put("userName", userName);
		mmap.put("postComplain", postComplain);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存帖子举报
	 */
	@RequiresPermissions("postComplain:postComplain:edit")
	@Log(title = "帖子举报", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PostComplain postComplain)
	{		
		return toAjax(postComplainService.updatePostComplain(postComplain));
	}
	
	/**
	 * 删除帖子举报
	 */
	@RequiresPermissions("postComplain:postComplain:remove")
	@Log(title = "帖子举报", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(postComplainService.deletePostComplainByIds(ids));
	}

//	@PostMapping("/ignoreVerify")
//	@ResponseBody
//	public AjaxResult ignoreVerify(Integer id) throws Exception {
//		PostComplain postComplain = postComplainService.selectPostComplainById(id);
//		postComplain.setComplainVerify("1");
//		postComplainService.updatePostComplain(postComplain);
//		Integer postId = postComplain.getPostId();
//		Post postPersist = postService.selectPostById(postId);
//		postPersist.setIsComplain(2);
//		return toAjax(postService.updatePost(postPersist));
//
//	}
//
//	@PostMapping("/hideVerify")
//	@ResponseBody
//	public AjaxResult hideVerify(Integer id) throws Exception {
//		PostComplain postComplain = postComplainService.selectPostComplainById(id);
//		postComplain.setComplainVerify("2");
//		postComplainService.updatePostComplain(postComplain);
//		Integer postId = postComplain.getPostId();
//		Post postPersist = postService.selectPostById(postId);
//		postPersist.setIsComplain(2);
//		postPersist.setVerifyState("2");
//		return toAjax(postService.updatePost(postPersist));
//	}
	
}
